package com.google.code.mukiranaproject.persistencia;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public abstract class Conexao {
	
	String url = "jdbc:mysql://127.0.0.1/MUKIRANA";
	String username = "root";
	String password = "admin";
	
	Connection con;
	
	public void abrirConexao() {
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection( url, username, password );
//			System.out.println("-----------conexao aberta----------- ");
		}catch (Exception e) {
			System.out.println("erro conexao banco de dados");
			e.printStackTrace();
		}
	}
	
	public void fecharConexao(){
		try {
			if(con != null && !con.isClosed())
				con.close();
//			System.out.println("-----------conexao fechada----------- ");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	/*
	 * Getters e Setters
	 */
	public Connection getConnection() {
		try {
			if(con == null || con.isClosed())
				abrirConexao();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}

	
}
